/*
 * Created: Author: Aléx Carvalho / Date: Feb 18, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 04:31:49 +0000 (Thu, 11 Jun 2015) $
 */
package com.cpm.view.zk.treetable.indicator;

import com.cpm.ServiceLocator;
import com.cpm.aggregate.SessionAttribute;
import com.cpm.context.SessionContext;
import com.cpm.entities.Indicator;
import com.cpm.view.zk.tree.DefaultTreeNode;
import com.cpm.view.zk.treetable.AbstractTreeTable;
import java.time.Year;
import java.util.Collection;
import java.util.Collections;
import org.zkoss.zul.Treecol;
import org.zkoss.zul.Treecols;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;

public class IndicatorTreeTable extends AbstractTreeTable<Indicator> {

    private final String[] NAMES = {
        "", "", "jan", "fev", "mar", "abr", "mai", 
        "jun", "jul", "ago", "set", "oct", "nov", "dez"
    };
    private final String[] HFLEX = {
        "2", "0", "0", "0", "0", "0", "0",
        "0", "0", "0", "0", "0", "0", "0"
    };

    private final String[] STYLE = {
        "background-color: rgb(51, 103, 152);",
        "background-color: rgb(51, 103, 152); border: none;",
        "", "", "", "", "", "", "", "", "", "", "", ""
    };

    private Collection<Indicator> indicators = Collections.EMPTY_LIST;
    
    public IndicatorTreeTable() {
        
        ServiceLocator.lookup(SessionContext.class).putAttribute(SessionAttribute.INDICATOR_TREE_TABLE_CURRENT_YEAR, Year.now());
        
        hiddenIconOpenCloseTree();
        openNodeOnSelect();
    }

    public void setIndicators(Collection<Indicator> indicators) {
        this.indicators = indicators != null ? indicators : Collections.EMPTY_LIST;
    }

    @Override
    protected void buildHeader(Treecols cols) {
        cols.getChildren().clear();

        String height = "25px";
        
        for (int colIndex = 0; colIndex < NAMES.length; colIndex++) {
            Treecol col = new Treecol(NAMES[colIndex]);
            col.setHflex(HFLEX[colIndex]);
            col.setAlign("center");
            col.setStyle(STYLE[colIndex] + " line-height: " + height + " !important;");

            cols.appendChild(col);
            col.setHeight(height);
        }
        
        cols.setHeight(height);
    }

    @Override
    protected void buildRootNode(DefaultTreeNode root) {

        indicators.stream().forEach((indicator) -> {
            DefaultTreeNode node = new DefaultTreeNode(new IndicatorRow(indicator));
            DefaultTreeNode target = new DefaultTreeNode(new IndicatorTargetRow(indicator));
            DefaultTreeNode result = new DefaultTreeNode(new IndicatorResultRow(indicator));

            node.add(target);
            node.add(result);
            root.add(node);
        });
    }

    @Override
    public void render(Treerow row, DefaultTreeNode node, int i) {
        Object data = node.getData();
        
        String rowStyle = "background-color: rgb(102, 154, 204);";
        
        if (data instanceof IndicatorRow) {
            rowStyle = ((Treeitem)row.getParent()).getIndex() % 2 == 0 ? "background-color: rgb(238, 238, 238);" : "";
            
            ((IndicatorRow) data).render(row, i);
        }
        else if (data instanceof IndicatorTargetRow) {
            ((IndicatorTargetRow) data).render(row, i);
        }
        else if (data instanceof IndicatorResultRow) {
            ((IndicatorResultRow) data).render(row, i);
        }
        
        row.setStyle(rowStyle);
    }
}
